package com.hzxy.yiyid.web.module.controller;

import com.hzxy.yiyid.web.common.base.BaseResult;
import com.hzxy.yiyid.web.common.constants.YiyidConstants;
import com.hzxy.yiyid.web.common.error.ErrorCodeEnum;
import com.hzxy.yiyid.web.module.controller.vo.ImportDataVO;
import com.hzxy.yiyid.web.module.service.DataManageService;
import com.hzxy.yiyid.web.tool.baidu.BaiduAI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName TestController
 * @Description 测试用
 * @Author RAICOM2023
 * @Date 2023/4/27 18:40
 * @Version 1.0
 **/
@Api(tags = "测试接口")
@Controller
@RequestMapping(value = "/test")
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    @Resource
    private DataManageService dataManageService;

    /*
     * @Author RAICOM2023
     * @Description //导入数据
     * @Date 15:14 2023/3/6
     * @Param [importDataVO, request]
     * @return com.hzxy.yiyid.web.common.base.BaseResult<?>
     **/
    @ApiOperation(value = "测试导入数据",notes = "测试导入数据")
    @RequestMapping(value = "/importData", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<?> importData(ImportDataVO importDataVO, HttpServletRequest request){
        Integer dataType = importDataVO.getDataType();
        if(null==dataType){
            return BaseResult.error(ErrorCodeEnum.REQUEST_PARAM_NULL_ERROR.getErrorCode(),ErrorCodeEnum.REQUEST_PARAM_NULL_ERROR.getMsg());
        }
        try{
            if (!(request instanceof MultipartHttpServletRequest)) {
                return BaseResult.error(ErrorCodeEnum.REQUEST_TYPE_ERROR.getErrorCode(),ErrorCodeEnum.REQUEST_TYPE_ERROR.getMsg());
            }
            MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
            if ( null == file ) {
                return BaseResult.error(ErrorCodeEnum.UPLOAD_FILE_NULL_ERROR.getErrorCode(),ErrorCodeEnum.UPLOAD_FILE_NULL_ERROR.getMsg());
            }
            String fileName = file.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf(".") + 1);
            if (null == suffixName
                    || (!suffixName.toLowerCase().matches(YiyidConstants.SUFFIX.EXCEL) && !suffixName.toLowerCase().matches(YiyidConstants.SUFFIX.CSV))) {
                return BaseResult.error(ErrorCodeEnum.UPLOAD_FILE_SUFFIX_ERROR.getErrorCode(),ErrorCodeEnum.UPLOAD_FILE_SUFFIX_ERROR.getMsg());
            }
            return dataManageService.uploadData(dataType,file);
        }catch(Exception e) {
            logger.error(ErrorCodeEnum.SYSTEM_ERROR.getMsg(), e);
            return BaseResult.error(ErrorCodeEnum.SYSTEM_ERROR.getErrorCode(), ErrorCodeEnum.SYSTEM_ERROR.getMsg());
        }
    }

    /*
     * @Author RAICOM2023
     * @Description //测试百度AI接口
     * @Date 15:17 2023/3/6
     * @Param []
     * @return void
     **/
    @ApiOperation(value = "测试百度AI",notes = "测试百度AI")
    @RequestMapping(value = "/testBaiduAi", method = RequestMethod.GET)
    @ResponseBody
    public void testBaiduAi(){
        try{
            JSONObject res = BaiduAI.NLP_ADDRESS("杭州市西湖区翠苑三区育新路16号\n");
            System.out.println(res.toString(2));
        }catch(Exception e){
            logger.error("错误信息："+e);
        }
    }

    /*
     * @Author RAICOM2023
     * @Description //TODO
     * @Date 0:37 2023/7/18
     * @Param []
     * @return void
     **/

    @ApiOperation(value = "导入CSv",notes = "导入CSv")
    @RequestMapping(value = "/importCsv", method = RequestMethod.GET)
    @ResponseBody
    public void importCsv(){
        try{
            dataManageService.readMedicineData(null);
            dataManageService.readDiseaseData(null);
            dataManageService.readSymptomData(null);
        }catch(Exception e){
            logger.error("错误信息："+e);
        }
    }
}
